/**  
 * All rights Reserved, Designed By daiw
 * @Title:  SysDeptServiceImpl.java   
 * @Package org.easy.system.manage.service.impl   
 * @Description:    TODO (用一句话描述该文件做什么)   
 * @author: daiw     
 * @date:   2019年10月21日 上午10:42:58   
 * @Copyright: 2019 daiw Inc. All rights reserved. 
 */
package org.easy.system.manage.service.impl;

import java.util.Date;
import java.util.List;

import org.easy.system.manage.dao.SysDeptMapper;
import org.easy.system.manage.dto.DeptDto;
import org.easy.system.manage.dto.DeptQueryParam;
import org.easy.system.manage.dto.SaveDeptParam;
import org.easy.system.manage.entity.SysDept;
import org.easy.system.manage.enums.DeptStatusEnum;
import org.easy.system.manage.service.SysDeptService;
import org.easyframework.core.entity.ProcessResult;
import org.easyframework.core.utils.BeanUtil;
import org.easyframework.core.utils.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.system.manage.facade.entity.UserInfoDto;

import com.github.pagehelper.PageHelper;

/**
 * @ClassName: SysDeptServiceImpl
 * @Description: 部门服务实现类
 * @author: daiw
 * @date: 2019年10月21日 上午10:42:58
 */
@Service
public class SysDeptServiceImpl implements SysDeptService {

	@Autowired
	private SysDeptMapper sysDeptMapper;

	@Override
	public ProcessResult<DeptDto> queryTopDept() {
		List<SysDept> depts = sysDeptMapper.queryByParentId(1L);
		DeptDto topNode = BeanUtil.convert(depts.get(0), DeptDto.class);
		List<SysDept> childNodes = sysDeptMapper.queryByParentId(topNode.getDeptId());
		topNode.setChildNodes(BeanUtil.convertList(childNodes, DeptDto.class));
		return ProcessResult.getInstSuccess(topNode);
	}

	@Override
	public ProcessResult<List<DeptDto>> queryByParentId(Long parentId) {
		List<SysDept> depts = sysDeptMapper.queryByParentId(parentId);
		return ProcessResult.getInstSuccess(BeanUtil.convertList(depts, DeptDto.class));
	}

	@Override
	public ProcessResult<?> save(SaveDeptParam saveDeptParam, UserInfoDto userInfo) {
		ProcessResult<String> validateResult = ValidationUtils.validate(saveDeptParam);
		if (validateResult.isFailed()) {
			return validateResult;
		}
		SysDept entity = BeanUtil.convert(saveDeptParam, SysDept.class);
		SysDept parent = sysDeptMapper.selectByPrimaryKey(entity.getParentId());
		entity.setAncestors(parent.getAncestors() + "," + parent.getDeptId());
		entity.setStatus(DeptStatusEnum.ACTIVE.getIndex());
		entity.setCreateBy(userInfo.getLoginName());
		entity.setCreateTime(new Date());
		int count = sysDeptMapper.insert(entity);
		return ProcessResult.getInstSuccess(count > 0);
	}

	@Override
	public ProcessResult<?> update(DeptDto deptDto, UserInfoDto userInfo) {
		SysDept updateCondition = BeanUtil.convert(deptDto, SysDept.class);
		updateCondition.setUpdateBy(userInfo.getLoginName());
		updateCondition.setUpdateTime(new Date());
		int count = sysDeptMapper.updateByPrimaryKeySelective(updateCondition);
		return ProcessResult.getInstSuccess(count > 0);
	}

	@Override
	public ProcessResult<List<DeptDto>> queryByPage(DeptQueryParam deptQueryParam) {
		PageHelper.offsetPage(deptQueryParam.getPageIndex(), deptQueryParam.getPageSize(), true);
		List<SysDept> detps = sysDeptMapper.queryByPage(deptQueryParam);
		List<DeptDto> dtos = BeanUtil.convertList(detps, DeptDto.class);
		return ProcessResult.getInstSuccess(dtos);
	}

	@Override
	public ProcessResult<DeptDto> queryDetail(Long deptId) {
		SysDept sysDept = sysDeptMapper.selectByPrimaryKey(deptId);
		DeptDto dto = BeanUtil.convert(sysDept, DeptDto.class);
		return ProcessResult.getInstSuccess(dto);
	}

	@Override
	public ProcessResult<?> updateStatus(Long deptId, Integer status, UserInfoDto userInfo) {
		SysDept updateCondition = new SysDept();
		updateCondition.setDeptId(deptId);
		updateCondition.setStatus(status);
		updateCondition.setUpdateBy(userInfo.getLoginName());
		updateCondition.setUpdateTime(new Date());
		int count = sysDeptMapper.updateByPrimaryKeySelective(updateCondition);
		return ProcessResult.getInstSuccess(count > 0);
	}
}
